package com.wuxinwudai.android.app;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.ViewGroup;
import android.view.Window;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.wuxinwudai.android.util.SharedPreferencesUtils;


/**
 * BaseStartActivity 类为通用启动页面 Activity 基类
 *
 * @author 吾心无待 于2016年02月27日
 */
public abstract class BaseStartActivity extends BaseActivity {
    private final static String IsFirstAppStart = "__IS_FIRST_APP_START_";

    private AlphaAnimation mAlphaAnimation;//透明动画
    private LinearLayout mView;//视图
    private ImageView mivImage;//图片
    private boolean isRunning;//是否在执行外部代码
    private boolean isAnimation;//动画中
    /**
     * 获取启动时显示的图片ID
     *
     * @return 启动时显示的图片 ID
     */
    public abstract int getStartImageResourceId();//反转控制

    /**
     * 获取将要跳转的 Activity 的类型
     * @return 要跳转的 Activity 的类型
     */
    public abstract Class getMainActivity();


    /**
     * 获取首次启动的引导 Activity 类型
     * @return 首次启动的引导的 Activity 类型
     */
    public abstract Class getGuideActivity();

    /**
     * 在显示启动页面时执行代码
     */
    public void execute(){

    }


    //进行跳转到 MainActivity
    private void goToMainActivity(){
        startActivity(new Intent(BaseStartActivity.this, getMainActivity()));
        finish();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try {
            boolean isFirstAppStart = !SharedPreferencesUtils.getInstance().getSharedPreferences().contains(IsFirstAppStart);
            if (isFirstAppStart) {
                SharedPreferencesUtils.putBoolean(IsFirstAppStart,true);
                startActivity(new Intent(BaseStartActivity.this, getGuideActivity()));
                finish();
            }else {
                supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
                mView = new LinearLayout(this);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
                mView.setLayoutParams(params);
                mivImage = new ImageView(this);
                mivImage.setLayoutParams(params);
                mivImage.setImageResource(getStartImageResourceId());
                mivImage.setScaleType(ImageView.ScaleType.CENTER_CROP);
                mView.addView(mivImage);
                setContentView(mView);
                mAlphaAnimation = new AlphaAnimation(0.6f, 1.0f);
                mAlphaAnimation.setDuration(2000);
                mAlphaAnimation.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {
                        isAnimation = true;
                        isRunning = true;
                        execute();
                        isRunning = false;
                        if (!isAnimation) {
                            goToMainActivity();
                        }
                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        isAnimation = false;
                        if (!isRunning) {
                            goToMainActivity();
                        }
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {

                    }
                });
                mivImage.startAnimation(mAlphaAnimation);
            }
        }catch (Exception ex){
            Log.i(TAG,ex.getMessage());
        }
    }
}
